/*
 * BusinessObjectEntityManagerFactory.java
 *
 * Created on 8 de Julho de 2007, 19:49
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package br.unifor.finance.business.bo.classes;

import br.unifor.finance.business.bo.BusinessObject;
import br.unifor.finance.business.bo.DespesaBusinessObject;
import br.unifor.finance.business.bo.TipoDespesaBusinessObject;
import br.unifor.finance.business.bo.UsuarioBusinessObject;
import br.unifor.finance.common.business.entity.Despesa;
import br.unifor.finance.common.business.entity.TipoDespesa;
import br.unifor.finance.common.business.entity.Usuario;
import br.unifor.finance.common.dto.PersistentObject;
import br.unifor.finance.persistence.dao.GenericDAO;
import br.unifor.finance.persistence.dao.jpa.GenericJPADAO;
import java.io.Serializable;
import java.util.Hashtable;
import java.util.Map;
import javax.persistence.EntityManager;

/**
 *
 * @author Clebernaice, Cleilson, Leonardo
 */
public class BusinessObjectEntityManagerFactory extends BusinessObjectAbstractFactory {
  
  private EntityManager emg;
  private Map<String, BusinessObject> mapCacheBO;
          
  /** Creates a new instance of BusinessObjectEntityManagerFactory */
  BusinessObjectEntityManagerFactory(EntityManager emg) {
    this.emg = emg;
    mapCacheBO = new Hashtable<String, BusinessObject>();
  }
  
  public <E extends PersistentObject, K extends Serializable> BusinessObject<E, K> getBusinessObject(Class<E> entityClass) {
    return null;
  }
  
  public synchronized DespesaBusinessObject getDespesaBO() {
    if (mapCacheBO.get(getKeyDespesaBO()) == null) {
      GenericDAO<Despesa, Integer> dao = new GenericJPADAO<Despesa, Integer>(emg);
      DespesaBusinessObjectImpl despesaBO = new DespesaBusinessObjectImpl(dao);
      mapCacheBO.put(getKeyDespesaBO(), despesaBO);
    }
    
    return (DespesaBusinessObject) mapCacheBO.get(getKeyDespesaBO());
  }

  public synchronized TipoDespesaBusinessObject getTipoDespesaBO() {
    if (mapCacheBO.get(getKeyTipoDespesaBO()) == null) {
      GenericDAO<TipoDespesa, Integer> dao = new GenericJPADAO<TipoDespesa, Integer>(emg);
      TipoDespesaBusinessObjectImpl tipoDespesaBO = new TipoDespesaBusinessObjectImpl(dao);
      mapCacheBO.put(getKeyTipoDespesaBO(), tipoDespesaBO);
    }
    
    return (TipoDespesaBusinessObject) mapCacheBO.get(getKeyTipoDespesaBO());
  }

  public UsuarioBusinessObject getUsuarioBO() {
    if (mapCacheBO.get(getKeyUsuarioBO()) == null) {
      GenericDAO<Usuario, Integer> dao = new GenericJPADAO<Usuario, Integer>(emg);
      UsuarioBusinessObjectImpl usuarioBO = new UsuarioBusinessObjectImpl(dao);
      mapCacheBO.put(getKeyUsuarioBO(), usuarioBO);
    }
    
    return (UsuarioBusinessObject) mapCacheBO.get(getKeyUsuarioBO());
  }
  
}
